package com.xinsmartsky.penmaji.penmaji;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.Handler;
import android.os.Handler.Callback;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.xinsmartsky.penmaji.Launch.MyProgress;

public class LoadingActivity extends Activity {
    private MyProgress launch_pgbar = null;
    private Handler mHandler;
    private PopupWindow popupWindow;
    private View launch_line_root;
    private SerialPortActivity serialport = new SerialPortActivity();
    private int devfd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        devfd = getIntent().getIntExtra("fd", 0);
        Log.i("fd", "launch===" + devfd);
        FindViewById();
        //加载页面弹出pop
        initPopupWindow();
        mHandler = new Handler(new Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                launch_pgbar.setProgress(msg.what);
                return false;
            }
        });
        ProgressThread();
        show();
        //发送开机命令？？返回键？？？电源键（上墨键）？？？？
        //确定：指向返回键
        serialport.CommuniProcess(devfd, (byte) 0xf8);
    }

    public void show() {
        launch_line_root.post(new Runnable() {
            @Override
            public void run() {
                if (popupWindow != null) {
                    popupWindow.showAtLocation(launch_line_root, Gravity.BOTTOM, 0, 0);
                }
            }
        });
    }

    private void FindViewById() {
        //进度条
        launch_pgbar = (MyProgress) findViewById(R.id.launch_pgbar);
        launch_line_root = findViewById(R.id.launch_line_root);//父窗口view
    }

    Intent intent = new Intent();

    private void ProgressThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i <= 100; i++) {
                    mHandler.sendEmptyMessage(i);
                    //100%后跳转到LoginActivity界面
                    if (i == 99) {
                        //读一次确定是否开机成功??????
                        Intent intent = new Intent();
                        Bundle bundle = new Bundle();
                        bundle.putInt("fd", devfd);
                        intent.putExtras(bundle);
                        intent.setClass(LoadingActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                    try {
                        Thread.sleep(6);//循环间隔时间600:1 min
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }


    //初始化
    private void initPopupWindow() {
        View contentView = getLayoutInflater().inflate(R.layout.activity_launcher_menu, null);//null??
        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setAnimationStyle(R.style.popupanimation);
    }

}
